<template>
  <a-row :gutter="24">
    <a-col :md="4" :sm="24">
      <a-card :bordered="false" :loading="treeLoading">
        <a-directory-tree
          :showIcon="false"
          :treeData="treeList"
          v-if="treeList.length"
          @select="handleClick"
          :defaultExpandAll="true"
        />
      </a-card>
    </a-col>

    <a-col :md="19" :sm="24">
      <a-card :bordered="false">
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="0">
              <a-col :md="6" :sm="24">
                <a-form-item label="所属行业">
                  <a-select style="width: 100%" v-model="queryParam.industryId" allow-clear placeholder="请选择所属行业">
                    <a-select-option v-for="(item,idx) in industryTypeList" :key="idx" :value="item.id">
                      {{ item.industryType }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="10" :sm="24">
                <span
                  class="table-page-search-submitButtons"
                  style="float:left;padding-left: 16px;">
                  <a-button type="primary" class="btn_search_reset" @click="$refs.table.refresh(true)"><a-icon
                    type="search"/>查询</a-button>
                  <a-button
                    style="margin-left: 8px"
                    class="btn_search_reset"
                    @click="() => {queryParam = {stationId:stationId};$refs.table.refresh(true);}"><a-icon
                      type="reload"/>重置</a-button>
                </span>
              </a-col>
              <a-col :md="8" :sm="24">
                <span
                  class="table-page-search-submitButtons"
                  style="float:right;">
                  <a-button
                    class="btn_add"
                    v-if="isShow()"
                    type="primary"
                    @click="$refs.addForm.add(stationId)"
                    icon="plus">新增</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>

        <s-table
          ref="table"
          size="middle"
          :columns="columns"
          :data="loadData"
          :alert="true"
          :rowKey="(record) => record.id"
        >

          <span slot="fileName" slot-scope="text,record" style="color: #fff">
            <a @click="lookImg(record, '设备正面')">预览</a>
            <a-modal :visible="previewVisible" :title="title" :footer="null" @cancel="previewCancel">
              <img alt="example" style="width: 100%" :src="previewImage" v-if="record.fileName"/>
              <p class="withoutData" v-else>暂无图片</p>
            </a-modal>
          </span>
          <span slot="backFileName" slot-scope="text,record" style="color: #fff">
            <a @click="lookImg(record, '设备反面')">预览</a>
            <a-modal :visible="backPreviewVisible" :title="title" :footer="null" @cancel="previewCancel">
              <img alt="example" style="width: 100%" :src="backPreviewImage"v-if="record.backFileName" />
              <p class="withoutData" v-else>暂无图片</p>
            </a-modal>
          </span>
          <span slot="nameplateFileName" slot-scope="text,record" style="color: #fff">
            <a @click="lookImg(record, '设备铭牌')">预览</a>
            <a-modal :visible="nameplatePreviewVisible" :title="title" :footer="null" @cancel="previewCancel">
              <img alt="example" :title="record.nameplateFileName" style="width: 100%" :src="nameplatePreviewImage" v-if="record.nameplateFileName != ''"/>
              <p class="withoutData" v-else>暂无图片</p>
            </a-modal>
          </span>

          <span slot="titleFilePath" slot-scope="text, record" style="color: #fff">
            <a @click="lookImg(record, '首页标题')">预览</a>
            <a-modal :visible="titleFilePreviewVisible" :title="title" :footer="null" @cancel="previewCancel">
              <img alt="example" style="width: 100%" :src="titleImage" v-if="titleImage != ''"/>
              <p class="withoutData" v-else>暂无图片</p>
            </a-modal>
          </span>

          <span slot="action" slot-scope="text, record">
            <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
            <a-divider type="vertical"/>
            <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
              <a class="btn_del">删除</a>
            </a-popconfirm>
            <a-divider type="vertical"/>
            <a @click="$refs.channelMarking.toIndex(record)" class="btn_details">通道标记</a>
          </span>
          <span slot="createdAt" slot-scope="text">
            {{ !!text ? text.substr(0,10): '' }}
          </span>
        </s-table>

        <add-form ref="addForm" @ok="handleOk"/>
        <edit-form ref="editForm" @ok="handleOk"/>
        <channel-marking ref="channelMarking" @ok="handleOk"/>

      </a-card>
    </a-col>
  </a-row>
</template>

<script>
import { STable } from '@/components'
import { page, del } from '@/api/modular/main/bizdevice/bizDeviceManage'
import addForm from './addForm'
import editForm from './editForm'
import channelMarking from './channelMarking'
import { treeList } from '@/api/modular/main/bizstation/bizStationManage'
import { industryTypeList } from '@/api/modular/main/bizindustryinvolved/bizIndustryInvolvedManage'

export default {
  components: {
    STable,
    addForm,
    editForm,
    channelMarking
  },

  data () {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {}, // 查询参数
      columns: [ // 表头
        {
          title: '产品型号',
          dataIndex: 'productModel',
          width: 100,
          ellipsis: true
        },
        {
          title: '产品厂商',
          dataIndex: 'productVendor'
        },
        {
          title: '设备名称',
          dataIndex: 'deviceName',
          width: 100,
          ellipsis: true
        },
        {
          title: '生产日期',
          dataIndex: 'createdAt',
          width: 120,
          align: 'center',
          scopedSlots: { customRender: 'createdAt' }
        },
        {
          title: '所属行业',
          dataIndex: 'industryType'
        },
        {
          title: '行业分组',
          dataIndex: 'groupId'
        },
        {
          title: '正面',
          dataIndex: 'fileName',
          scopedSlots: { customRender: 'fileName' },
          align: 'center'
        },
        {
          title: '反面',
          dataIndex: 'backFileName',
          scopedSlots: { customRender: 'backFileName' },
          align: 'center'
        },
        {
          title: '铭牌',
          dataIndex: 'nameplateFileName',
          scopedSlots: { customRender: 'nameplateFileName' },
          align: 'center'
        },
        {
          title: '首页标题',
          dataIndex: 'titleFilePath',
          scopedSlots: { customRender: 'titleFilePath' },
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          width: 250,
          align: 'center'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      replaceFields: {
        key: 'id'
      },
      treeList: [],
      treeLoading: true,
      index: -1,
      stationId: '',
      industryTypeList: [],
      url: window.CONST.VUE_APP_API_BASE_URL,
      previewVisible: false,
      backPreviewVisible: false,
      nameplatePreviewVisible: false,
      titleFilePreviewVisible: false,
      previewImage: '',
      backPreviewImage: '',
      nameplatePreviewImage: '',
      titleImage: '',
      fileUrl: window.CONST.VUE_APP_API_BASE_FILE_URL,
      title: ''
    }
  },

  created () {
    this.getStationTree()
    this.getIndustryTypeList()
  },

  methods: {
    del (record) {
      del(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败：' + res.message)
        }
      }).catch((err) => {
        this.$message.error('删除错误：' + err.message)
      })
    },
    getStationTree () {
      treeList(Object.assign(this.queryParam)).then(res => {
        this.treeLoading = false
        if (!res.success) {
          return
        }
        this.treeList = res.data
      })
    },
    getIndustryTypeList () {
      industryTypeList().then(res => {
        this.treeLoading = false
        if (!res.success) {
          return
        }
        this.industryTypeList = res.data
      })
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleClick (e) {
      const that = this
      if (e.length > 0) {
        that.stationId = e.toString()
        that.queryParam.stationId = e.toString()
        that.handleOk()
      } else {
        that.queryParam.stationId = ''
        that.handleOk()
      }
    },
    look (record) {
      window.open(this.url + '/admin/index1.html#/device_monitor?deviceId=' + record.id)
    },
    lookImg (record, face) {
      console.log(record)
      this.title = record.deviceName + '\xa0\xa0\xa0\xa0\xa0\xa0' + record.productModel + '\xa0\xa0\xa0\xa0\xa0\xa0' + face
      if (face === '设备正面') {
        this.previewImage = this.fileUrl + record.fileName
        this.previewVisible = true
      } else if (face === '设备反面') {
        this.backPreviewImage = this.fileUrl + record.backFileName
        this.backPreviewVisible = true
      } else if (face === '设备铭牌') {
        this.nameplatePreviewVisible = true
        this.nameplatePreviewImage = this.fileUrl + record.nameplateFileName
      } else if (face === '首页标题') {
        this.titleFilePreviewVisible = true
        this.titleImage = record.titleFilePath ? (this.fileUrl + record.titleFilePath) : ""
      }
    },
    previewCancel () {
      this.previewVisible = false
      this.backPreviewVisible = false
      this.nameplatePreviewVisible = false
      this.titleFilePreviewVisible = false
    },
    isShow () {
      if (this.stationId) {
        return true
      }
      return false
    }
  }
}
</script>

<style lang="less" scoped>
@import '../~@/assets/css/list.less';

.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
.table-page-search-wrapper{
  padding-left: 10px !important;
  padding-right: 10px;
  .ant-row{
    margin-left: 0px !important;
  }
}
  .withoutData{
    text-align: center;
    font-size: 1vw;
    color: #fff;
  }
</style>
